package com.itxiuyixiu.tools.util.returns;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

/**
 * @author 黄磊
 * @date 2020/4/29
 */
public class ReturnVo {
    /**
     * 状态码
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer status;
    /**
     * 返回信息
     * <p>
     * 如：报名成功
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String message;
    /**
     * 返回数据
     * <p>
     * 如：查询学生数据等
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Object data;

    protected ReturnVo() {
    }

    public ReturnVo(HttpStatus httpStatus, String message) {
        this(httpStatus.value(), message, null);
    }

    public ReturnVo(HttpStatus httpStatus, Object data) {
        this(httpStatus.value(), null, data);
    }

    public ReturnVo(HttpStatus httpStatus, String message, Object data) {
        this(httpStatus.value(), message, data);
    }

    public ReturnVo(Integer status, String message) {
        this(status, message, null);
    }

    public ReturnVo(Integer status, Object data) {
        this(status, null, data);
    }

    public ReturnVo(Integer status, String message, Object data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public static ReturnVo getSuccessReturn(String message) {
        return new ReturnVo(HttpStatus.OK, message);
    }

    public static ReturnVo getSuccessReturn(Object data) {
        return new ReturnVo(HttpStatus.OK, data);
    }

    public static ReturnVo getSuccessReturn(String message, Object data) {
        return new ReturnVo(HttpStatus.OK, message, data);
    }

    public static ReturnVo getFailureReturn(String message) {
        return new ReturnVo(HttpStatus.BAD_REQUEST, message);
    }

    public static ReturnVo getFailureReturn(String message, Object data) {
        return new ReturnVo(HttpStatus.BAD_REQUEST, message, data);
    }

    public static ReturnVo getFailureReturn(Integer code, String message) {
        return new ReturnVo(code, message);
    }

    public static ReturnVo getFailureReturn(HttpStatus httpStatus, String message) {
        return new ReturnVo(httpStatus, message);
    }

    public static ReturnVo getFailureReturn(Integer code, String message, Object data) {
        return new ReturnVo(code, message, data);
    }

    public static ReturnVo getFailureReturn(HttpStatus status, String message, Object data) {
        return new ReturnVo(status, message, data);
    }

    public static ReturnVo getReturn(HttpStatus httpStatus, String message, Object data) {
        return new ReturnVo(httpStatus, message, data);
    }

    public static ReturnVo getFailureReturn(BindingResult bindingResult) {
        StringBuilder stringBuilder = new StringBuilder();
        bindingResult.getAllErrors().forEach(o -> {
            FieldError error = (FieldError) o;
            stringBuilder.append(error.getField()).append(" : ").append(error.getDefaultMessage()).append("\n");
        });
        return new ReturnVo(HttpStatus.BAD_REQUEST, stringBuilder.toString());
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
